/*
 * Copyright (c) 2001-2008, TIBCO Software Inc.
 * Use, modification, and distribution subject to terms of license.
 */

(function(plugIn){

jsx3.require("jsx3.gui.Block");

/**
 * A wizard. Provided by the <code>jsx3.amp.util.wizard</code> plug-in.
 */
jsx3.Class.defineClass("com.tibco.amx.SwapPane", jsx3.gui.Block, null, function(SwapPane, SwapPane_prototype) {

  var amx = com.tibco.amx;
  var Block = jsx3.gui.Block;

  SwapPane.DEFAULT_MILLIS = 800;
  SwapPane._MILLIS_PER_STEP = 75;

jsx3.$O(SwapPane_prototype).extend({

  getActiveIndex: function() {
    var numChildren = this.getChildren().length;
    if (numChildren == 0)
      return -1;

    if (this.activeindex == null)
      this.activeindex = 0;

    return Math.max(0, Math.min(this.activeindex, numChildren - 1));
  },

  getActivePane: function() {
    return this.getChildren()[this.getActiveIndex()];
  },

  onAfterAttach: function() {
    var activeIndex = this.getActiveIndex();

    var c = this.getChildren();
    for (var i = 0; i < c.length; i++) {
      c[i].setWidth("100%");
      c[i].setHeight("100%");
      c[i].setDisplay(i == activeIndex ? Block.DISPLAYBLOCK : Block.DISPLAYNONE);
    }

    this.setOverflow(Block.OVERFLOWHIDDEN);
  },

  /**
   * Set the active pane in the swap pane by swiping the active one out and
   * @param intIndex
   * @param intMillis
   */
  setActivePane: function(intIndex, intMillis, callback) {
    if (intMillis == null)
      intMillis = SwapPane.DEFAULT_MILLIS;
    if (callback != null && typeof callback == "function")
      this.callback = callback;

    var activeIndex = this.getActiveIndex();
    jsx3.log('setActivePane['+intIndex+']:activeIndex['+activeIndex+']');
    if (activeIndex != intIndex) {
      var newChild = this.getChildren()[intIndex];

      if (newChild) {
        var oldChild = this.getChildren()[activeIndex];

        if (oldChild) {
          var direction = activeIndex > intIndex ? -1 : 1;
          this._animate(newChild, oldChild, direction, intMillis);
        } else {
          newChild.setDisplay(Block.DISPLAYBLOCK, true);
        }

        this.activeindex = intIndex;
      }
    }
  },

  _animate: function(newChild, oldChild, direction, intMillis) {
    newChild.setRelativePosition(Block.ABSOLUTE, true);
//    newChild.setLeft((direction * 100) + "%", true);
    newChild.getRendered().style.left = (direction * 100) + "%";
    newChild.setDisplay(Block.DISPLAYBLOCK, true);

    oldChild.setRelativePosition(Block.ABSOLUTE, true);

    var numSteps = Math.ceil(intMillis / SwapPane._MILLIS_PER_STEP);
    this._step(newChild, oldChild, direction, 1, numSteps, SwapPane._MILLIS_PER_STEP);
  },

  _step: jsx3.$F(function(newChild, oldChild, direction, intStep, intTotalSteps, intTimeout) {
    var ratio = this._getRatioDone(intStep, intTotalSteps);
    var pct = Math.round(100 * ratio);

    oldChild.getRendered().style.left = (direction * pct) + "%";
    newChild.getRendered().style.left = (-1 * direction * (100 - pct)) + "%";

    if (intStep == intTotalSteps) {
      oldChild.setDisplay(Block.DISPLAYNONE, true);
      oldChild.setRelativePosition(Block.RELATIVE, true);
      oldChild.setLeft(0, true);

      newChild.setRelativePosition(Block.RELATIVE, true);
      if (this.callback) // hook for implement some functionality when swap completed.  -- Turner
        this.callback.apply(this, [newChild, oldChild]);
    } else {
      var newParams = [newChild, oldChild, direction, intStep + 1, intTotalSteps, intTimeout];
      window.setTimeout(this._step.bind(this, newParams), intTimeout);
    }
  }),

  _getRatioDone: function(intDone, intTotal) {
    // Modify this method for easing...
    return intDone / intTotal;
  }

});

});

})(this);
